package com.geese.module.admin.service.impl;

import com.geese.common.constants.Constants;
import com.geese.common.entity.Business;
import com.geese.common.entity.Courier;
import com.geese.common.entity.Order;
import com.geese.common.vo.Page;
import com.geese.module.admin.service.IOrderAdminService;
import com.geese.module.admin.vo.OrderVO;
import com.geese.module.business.dao.BusinessMapper;
import com.geese.module.business.vo.BusinessVO;
import com.geese.module.ceo.dao.CourierMapper;
import com.geese.module.ceo.dao.OrderMapper;
import com.geese.module.ceo.vo.WaiterQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by huweiwei on 2017/3/28.
 */
@Service
public class OrderAdminServiceImpl implements IOrderAdminService {

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CourierMapper courierMapper;

    @Override
    public Map<String, Object> getList(Page page, OrderVO orderVO) {
        List<Order> orderList = getOrder(page, orderVO);
        List<Map<String, Object>> data = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(orderList)) {
            List<Integer> businessIds = Lists.transform(orderList, new Function<Order, Integer>() {
                @Override
                public Integer apply(Order order) {
                    return order.getBusinessId();
                }
            });
            List<Integer> courierIds = Lists.transform(orderList, new Function<Order, Integer>() {
                @Override
                public Integer apply(Order order) {
                    return order.getCourierId();
                }
            });
            List<Courier> courierList = courierMapper.selectByIdsIgnoreDel(courierIds);
            List<Business> businessList = businessMapper.selectByIdsIgnoreDel(businessIds);

            Map<Integer, Courier> courierMap = Maps.uniqueIndex(courierList, new Function<Courier, Integer>() {
                @Override
                public Integer apply(Courier courier) {
                    return courier.getId();
                }
            });
            Map<Integer, Business> businessMap = Maps.uniqueIndex(businessList, new Function<Business, Integer>() {
                @Override
                public Integer apply(Business business) {
                    return business.getId();
                }
            });

            for (Order order : orderList) {
                Map<String, Object> temp = Maps.newHashMap();
                int status = order.getStatus();
                temp.put("id", order.getId());
                temp.put("code", order.getCode());
                temp.put("businessName", order.getBusinessName());
                temp.put("businessMobile", businessMap.get(order.getBusinessId()).getMobile());
                temp.put("businessAmount", businessMap.get(order.getBusinessId()).getSingleAmount());
                if (status == 0) {
                    temp.put("courierName", "-");
                    temp.put("courierMobile", "-");
                } else {
                    temp.put("courierName", order.getCourierName());
                    temp.put("courierMobile", courierMap.get(order.getCourierId()).getMobile());
                }
                temp.put("courierAmount", order.getOrderAmount());
                temp.put("status", status);
                temp.put("imgUrl", order.getImageUrl());
                temp.put("completeTime", order.getCompleteTime());
                temp.put("receiveTime", order.getReceiveTime());
                temp.put("sendTime", order.getSendTime());
                temp.put("adminAmount", businessMap.get(order.getBusinessId()).getSingleAmount() - order.getOrderAmount());
                data.add(temp);
            }
        }

        PageInfo pages = new PageInfo(orderList);
        Map<String, Object> pageInfo = Maps.newHashMap();
        pageInfo.put(Constants.CURR_PAGE, pages.getPageNum());
        pageInfo.put(Constants.TOTAL_PAGE, pages.getPages());
        pageInfo.put(Constants.TOTAL, pages.getTotal());

        Map<String, Object> result = Maps.newHashMap();
        result.put(Constants.PAGE_INFO, pageInfo);
        result.put("orderList", data);
        return result;
    }

    /**
     * 根据查询条件获取订单列表
     * @param page
     * @param orderVO
     * @return
     */
    private List<Order> getOrder(Page page, OrderVO orderVO) {
        String businessMobile = orderVO.getBusinessMobile();
        String courierMobile = orderVO.getCourierMobile();
        List<Order> orderList;
        if (StringUtils.isNotBlank(businessMobile)) {
            BusinessVO businessVO = new BusinessVO();
            businessVO.setMobile(businessMobile);
            List<Business> businessList = businessMapper.findAll(businessVO);
            if (CollectionUtils.isEmpty(businessList)) {
                orderList = Lists.newArrayList();
            } else {
                List<Integer> businessIds = Lists.transform(businessList, new Function<Business, Integer>() {
                    @Override
                    public Integer apply(Business business) {
                        return business.getId();
                    }
                });
                PageHelper.startPage(page.getCurrPage(), page.getPageSize(), " status ");
                orderList = orderMapper.selectOrderByBusinessIds(businessIds);
            }
        } else if (StringUtils.isNotBlank(courierMobile)) {
            WaiterQuery waiterQuery = new WaiterQuery();
            waiterQuery.setMobile(courierMobile);
            List<Courier> courierList = courierMapper.listWaiterByQuery(waiterQuery);
            if (CollectionUtils.isEmpty(courierList)) {
                orderList = Lists.newArrayList();
            } else {
                List<Integer> courierIds = Lists.transform(courierList, new Function<Courier, Integer>() {
                    @Override
                    public Integer apply(Courier courier) {
                        return courier.getId();
                    }
                });
                PageHelper.startPage(page.getCurrPage(), page.getPageSize(), " status ");
                orderList = orderMapper.selectOrderByCourierIds(courierIds);
            }
        } else {
            PageHelper.startPage(page.getCurrPage(), page.getPageSize(), " status ");
            orderList = orderMapper.selectOrderByOrderVO(orderVO);
        }
        return orderList;
    }
}
